//城市选择
var app = getApp();
Page({
  data: {
    shengArr: [],//省级数组
    shengId: [],//省级id数组
    shiArr: [],//城市数组
    shiId: [],//城市id数组
    quArr: [],//区数组
    shengIndex: 0,
    shiIndex: 0,
    quIndex: 0,
    mid: 0,
    sheng: 0,
    city: 0,
    area: 0,
    code: 0,
    cartId: 0,
    location: [],
    center_latitude: '',
    center_longitude: '',
    markers: [],
    covers: [],
    circles: [],
    out_distance:false,
    business:0,
  },
  chooseLocation: function () {
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        //console.log(res)
        that.setData({
          covers: [{
            iconPath: "/images/location.png",
            latitude: res.latitude,
            longitude: res.longitude,
          }],
          locationAddress: res.address,
          locationName: res.name
        })

        var distance = that.getDistance(that.data.center_latitude, that.data.center_longitude, res.latitude, res.longitude);
        if (distance > (that.data.business.xianding * 1000)){
          that.setData({
            out_distance:true
          });
          wx.showToast({
            title: '不在配送范围',
            duration: 1000
          });
          return;
        }else{
          that.setData({
            out_distance: false
          });
        }

      }
    })
  },
  chooseLocation2: function () {
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        //console.log(res)
        that.setData({
          covers: [{
            iconPath: "/images/location.png",
            latitude: res.latitude,
            longitude: res.longitude,
          }],
          locationAddress: res.address,
          locationName: res.name
        })

        that.setData({
          out_distance: false
        });

      }
    })
  },
  getDistance: function (lat1, lng1, lat2, lng2) {
    lat1 = lat1 || 0;
    lng1 = lng1 || 0;
    lat2 = lat2 || 0;
    lng2 = lng2 || 0;
    var rad1 = lat1 * Math.PI / 180.0;
    var rad2 = lat2 * Math.PI / 180.0;
    var a = rad1 - rad2;
    var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
    var r = 6378137;
    return (r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2)))).toFixed(0);//单位：米

  },
  formSubmit: function (e) {
    var that = this;
    var adds = e.detail.value;
    var nlen = adds.name.length;
    if (nlen < 2 || nlen > 10) {
      wx.showToast({
        title: '收货人姓名有误',
        duration: 1000
      });
      return;
    }
    var phone = adds.phone;
    if (!(/^1[34578]\d{9}$/.test(phone))) {
      wx.showToast({
        title: '手机号码有误',
        duration: 1000
      });
      return;
    }
    var alen = adds.address.length;
    if (alen < 5 || alen > 100) {
      wx.showToast({
        title: '详细地址有误',
        duration: 1000
      });
      return;
    }
    var mlen = adds.menpai.length;
    if (mlen < 1 || mlen > 100) {
      wx.showToast({
        title: '门牌号有误！',
        duration: 1000
      });
      return;
    }

    var cartId = this.data.cartId;
    wx.request({
      url: app.d.hostUrl + 'wapi/add_address.html',
      data: {
        seesionid: app.globalData.userInfo.sessionId,
        openid: app.globalData.userInfo.openid,
        receiver: adds.name,
        tel: phone,
        adds: adds.address,
        menpai: adds.menpai,
        code: this.data.code,
        locationname:adds.locationname
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {// 设置请求的 header
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        // success
        var status = res.data.ret;
        if (status == 0) {
          wx.showToast({
            title: '保存成功！',
            duration: 2000
          });
          if(cartId != undefined && cartId != 0){
            wx.redirectTo({
              //url: 'user-address/user-address?cartId=' + cartId
              url: 'user-address/user_address?cartId=' + cartId
            });
          }else{
            var vip = that.data.vip;
            wx.redirectTo({
              //url: 'user-address/user-address?cartId=' + cartId
              url: 'user-address/user_address?free=1&back=1&vip=' + vip
            });
          }
          
        } else {
          wx.showToast({
            title: res.data.err,
            duration: 2000
          });
        }
      },
      fail: function () {
        // fail
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    })


  },
  onLoad: function (options) {
    // 生命周期函数--监听页面加载
    var that = this; console.log(options); console.log(options.cartId);
    if (options.cartId != undefined && options.cartId  != 0){console.log(444);
      that.setData({ cartId: options.cartId})
    }else{
      that.setData({ vip: options.vip })
    }
  },

  bindPickerChangeshengArr: function (e) {
    this.setData({
      shengIndex: e.detail.value,
      shiArr: [],
      shiId: [],
      quArr: [],
      quiId: []
    });
    var that = this;
    wx.request({
      url: app.d.hostUrl + 'wapi/get_province.html',
      data: { province: e.detail.value },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {// 设置请求的 header
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        // success
        var status = res.data.status;
        var city = res.data.data;

        var hArr = [];
        var hId = [];
        hArr.push('请选择');
        hId.push('0');
        for (var i = 0; i < city.length; i++) {
          hArr.push(city[i].name);
          hId.push(city[i].id);
        }
        that.setData({
          //sheng:res.data.sheng,
          sheng: e.detail.value,
          shiArr: hArr,
          shiId: hId
        })
      },
      fail: function () {
        // fail
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      },

    })
  },
  bindPickerChangeshiArr: function (e) {
    this.setData({
      shiIndex: e.detail.value,
      quArr: [],
      quiId: []
    })
    var that = this;
    wx.request({
      url: app.d.hostUrl + 'wapi/get_area_name.html',
      data: {
        city: e.detail.value,
        province: this.data.sheng //province
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {// 设置请求的 header
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var status = res.data.status;
        var area = res.data.data;

        var qArr = [];
        var qId = [];
        qArr.push('请选择');
        qId.push('0');
        for (var i = 0; i < area.length; i++) {
          qArr.push(area[i].name)
          qId.push(area[i].id)
        }
        that.setData({
          //city:res.data.city,
          city: e.detail.value,
          quArr: qArr,
          quiId: qId
        })
      },
      fail: function () {
        // fail
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    })
  },
  bindPickerChangequArr: function (e) {
    this.setData({
      quIndex: e.detail.value
    });
    var that = this;
    wx.request({
      url: app.d.hostUrl + 'wapi/get_code.html',
      data: {
        quyu: e.detail.value,
        city: this.data.city
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {// 设置请求的 header
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        that.setData({
          //area:res.data.area,
          area: e.detail.value,
          //code:res.data.code
        })
      },
      fail: function () {
        // fail
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    })
  },

  onShareAppMessage: function () {
    return {
      title: '极好生活商城',
      path: '/pages/index/index?scene=invite%3D' + app.globalData.user_id,
      success: function (res) {
        wx.showToast({
          title: '分享成功',
          duration: 2000
        });
      },
      fail: function (res) {
        // 分享失败
      }
    }
  },
})